<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类管理</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>

<body>
    <div class="layui-container-fluid">
        <div class="nav">
            <span class="layui-breadcrumb">
                <a href="/dashBoard">首页</a>
                <a><cite>分类管理</cite></a>
            </span>
        </div>
        <table class="layui-table">
            <thead>
                <tr>
                    <th>分类名称</th>
                    <th>分类描述</th>
                    <th>分类图标</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <% if(!cates.length){%>
                    <tr>
                        <td align="center" colspan="4">暂无分类</td>
                    </tr>
                 <% }%>
               
                <% for(var a=0;a<cates.length;a++){ %>
                    <tr>
                        <td>
                            <%= cates[a].cateName %>
                        </td>
                        <td title="<%= cates[a].cateDesc %>">
                            <%= cates[a].cateDesc ? cates[a].cateDesc.substr(0,10)+'...': '暂无描述' %>
                        </td>
                        <td>
                            <img src="<%= cates[a].cateIcon || 'https://img2.baidu.com/it/u=26528730,97133990&fm=15&fmt=auto&gp=0.jpg' %>"
                                width="100" alt="">

                        </td>
                        <td>
                            <a href="/cateUpdate?id=<%= cates[a]._id %>" class="layui-btn layui-btn-xs">编辑</a>
                            <button _id="<%= cates[a]._id%>" type="button"
                                class="layui-btn del layui-btn-danger layui-btn-xs">删除</button>
                        </td>
                    </tr>
                    <% }%>

            </tbody>
        </table>
        <div id="page"></div>
        <a href="/cateAdd" class="layui-btn layui-btn-normal">增加</a>
    </div>
    <script src="/static/lib/layui/layui.js"></script>
    <script>
        layui.use(['laypage', 'jquery', 'layer'], function () {
            var laypage = layui.laypage;
            var $ = layui.jquery;
            var layer = layui.layer;

            //执行一个laypage实例
            laypage.render({
                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                , count: <%= counts %> //数据总数，从服务端得到
               , limit: 4,
                curr:<%=current %>,
                jump: function (obj, first) {
                    //obj包含了当前分页的所有参数，比如：


                    //首次不执行
                    if (!first) {
                        //do something
                        location.href = "/cateList?current=" + obj.curr + "&pageSize=" + obj.limit
                    }
                }
            });
        $('.del').click(function () {
            var id = $(this).attr('_id')

            layer.confirm('删除分类?', { icon: 3, title: '您确定删除吗' }, function (index) {
                //do something
                $.ajax({
                    url: '/delCate',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        id: id
                    },
                    success: function (res) {
                        if (res.code ===0) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                history.go(0)
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            })
                        }

                    }
                })


                layer.close(index);
            });
        })
        });
    </script>
</body>

</html>